package com.skytech.component.crawler.task;

import com.skytech.component.crawler.scratch.CrawlerUrl;
import com.skytech.component.crawler.scratch.CrawlerUrlDAO;
import com.skytech.component.crawler.scratch.TargetRules;
import com.skytech.component.crawler.util.ElementAbstractUrl;
import com.skytech.component.crawler.util.ListRegex;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.data.domain.Example;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 新闻类页面列表页解析
 */
public class NewsListPageOperation implements IndexPageOperation {

    private CrawlerUrlDAO crawlerUrlDAO;

    public NewsListPageOperation(CrawlerUrlDAO crawlerUrlDAO){
        this.crawlerUrlDAO = crawlerUrlDAO;
    }

    @Override
    public boolean operate(ChromeDriver driver, TargetRules targetRules) {
        String html = driver.findElementByTagName("html").getAttribute("outerHTML");

        Document parse = Jsoup.parse(html, driver.getCurrentUrl());
        Elements allElements = parse.getAllElements();
        List<String> content = new ArrayList<>();
        if (!StringUtils.isEmpty(targetRules.getContentRegex())) {
            content = ListRegex.regex(ElementAbstractUrl.links(allElements), targetRules.getContentRegex());
        } else if (!StringUtils.isEmpty(targetRules.getContentElement())) {
            Elements elements = allElements.select(targetRules.getContentElement());
            if (!elements.isEmpty()) {
                content = ElementAbstractUrl.links(elements.get(0).getAllElements());
            }
        } else {
            return false;
        }
        CrawlerUrl crawlerUrl = new CrawlerUrl();
        for (String contentUrl : content) {
            crawlerUrl.setContentUrl(contentUrl);
            if (!crawlerUrlDAO.findAll(Example.of(crawlerUrl)).isEmpty()) {
                continue;
            }
            CrawlerUrl c = new CrawlerUrl();
            c.setId(UUID.randomUUID().toString());
            c.setContentUrl(contentUrl);
            c.setDomainUrl(driver.getCurrentUrl());
            c.setRuleId(targetRules.getId());
            c.setParse("no");
            c.setOperateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            crawlerUrlDAO.save(c);
        }
        return true;
    }
}
